import React, { Component } from 'react';
import { Radio } from 'antd';
import PropTypes from 'prop-types';

class FormRadio extends Component {
  static defaultProps = {
    onChange: () => {}
  };

  static propTypes = {
    node: PropTypes.object.isRequired,
    value: PropTypes.any,
    onChange: PropTypes.func.isRequired
  };

  renderStaticOptions = style => {
    const options = this.props.getData('datasource');
    if (!Array.isArray(options)) {
      return null;
    }
    return options.map(option => (
      <Radio style={style} value={option} key={option}>
        {option}
      </Radio>
    ));
  };

  render() {
    const { getData, value, onChange } = this.props;
    const defaultValue = getData('defaultValue');
    const direction = getData('direction');
    const radioStyle =
      direction === 'vertical'
        ? {
            display: 'block',
            height: '30px',
            lineHeight: '30px'
          }
        : undefined;
    return (
      <Radio.Group
        value={value}
        defaultValue={defaultValue}
        onChange={onChange}
      >
        {this.renderStaticOptions(radioStyle)}
      </Radio.Group>
    );
  }
}

export default FormRadio;
